package main.ressik;


import java.util.ArrayList;

import android.app.AlertDialog;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

public class FoodDb {
	
	public static final String FOOD_DB_NAME = "FoodTracker";
	public static final String FOOD_TABLE_NAME = "Foods";
	public static final String DATE_TABLE_NAME = "Dates";
	private static FoodDb instance;
	private static Main main;
	public static SQLiteDatabase db;
	
	
	
	
	
	/**
	 * Returns the already created FoodDb object.  
	 */
	public static FoodDb getInstance(){
		if (instance == null){
			instance = new FoodDb();
		}
		return instance;
	}
	
	
	
	
	
	/**
	 * Private Constructor.  Any instantiations of this object should go through
	 * getInstance().
	 */
	private FoodDb(){
		instance = this;
		main = Main.getInstance();
	}
	
	
	
	
	
    /**
     * Adds a particular food into the DB
     */
    public void addFood(String food, String cals, String fat, String fiber){
    	db.execSQL("INSERT INTO " + FOOD_TABLE_NAME + " VALUES('" + food + "','" + cals + "','" + fat + "','" + fiber + "')");
		//refreshTextComplete();
    }
    
    
    
    
    
    /**
     * Adds the food object to the DateDb database.
     */
    public void addFoodToDate(String food, String date){
    	db.execSQL("INSERT INTO " + DATE_TABLE_NAME + " VALUES('" + food + "','" + date + "')");
    }
    
    
    
    
    
    /**
     * Gets the foods for a particular day.
     */
    public ArrayList<String> getFoodsForDay(String date){
    	ArrayList<String> foods = new ArrayList<String>();
		Cursor c = db.query(DATE_TABLE_NAME, null, "date='" + date + "'", null, null, null, "lower (name) ASC");
	if (c != null){
    	while(c.moveToNext()){
    		int foodIndex = c.getColumnIndex("name"); 
    		String food = c.getString(foodIndex);
    		foods.add(food);
    	}
	}
	return foods;
    }
    
    
    
    
    
    /**
     * Gets the cursor object for foods.
     */
    public Cursor getFoodCursorForDay(String date){
    	Cursor c = db.query(DATE_TABLE_NAME, null, "date='" + date + "'", null, null, null, "name ASC");
    	return c;
    }
    
    
    
    
    
    /**
     * Gets all the foods in the Database.  Then returns the names as a List of Strings.
     */
    public ArrayList<String> getFoodsInDb(){
    	ArrayList<String> foods = new ArrayList<String>();
    		Cursor c = db.query(FOOD_TABLE_NAME, null, null, null, null, null, "lower (name) ASC");
    	if (c != null){
	    	while(c.moveToNext()){
	    		int foodIndex = c.getColumnIndex("name"); 
	    		String food = c.getString(foodIndex);
	    		foods.add(food);
	    	}
    	}
    	return foods;
    }
    
    
    
    
    
    /**
     * Deletes a food from the DateDb database.  This only removes 1 matching object if multiple are found.
     */
    public void deleteFoodFromDay(String food, String date){
    	int rows = 0;
    	Cursor foodsInDate = db.rawQuery("SELECT * FROM " + DATE_TABLE_NAME + " WHERE name='" + food + "' AND date='" + date + "'", null);
    	if (foodsInDate != null){
    		while (foodsInDate.moveToNext()){
        		rows ++;
        	}
    	}
    	db.execSQL("DELETE FROM " + DATE_TABLE_NAME+ " WHERE name='" + food + "' AND date= '" + date + "'");
    	for (int i = 1; i<rows; i++){
    		addFoodToDate(food, date);
    	}
    }
    
    
    
    
    
    /**
     * Deletes a particular food from the Database.
     */
    public void deleteFood(String food){
		db.execSQL("DELETE FROM " + DATE_TABLE_NAME+ " WHERE name='" + food + "'");
		int deletedRows = db.delete(FOOD_TABLE_NAME, "name='" + food + "'", null);
    	if (deletedRows != 1){
    		new AlertDialog.Builder(main).setTitle("Error:").setMessage("There was a problem deleting your food item.  It appears either more than one, or NO items were deleted.").show();
    	}
    }
    
    
    
    
    
    /**
     * Checks to see is a particular food exists in the DateDB.  If so, it returns true.
     */
    public boolean foodExistsInDate(String food){
    	Cursor c = db.query(DATE_TABLE_NAME, null, "name='" + food + "'", null, null, null, "name ASC");
    	if (c.getCount() != 0){
    		return true;
    	}
    	return false;
    }
    
    
    
    
    
    /**
     * Takes a name of the food as a parameter and returns a Cursor with information from that food.
     */
    public Cursor getSingleFood(String name){
    	Cursor c = db.query(FOOD_TABLE_NAME, null, "name='" + name + "'", null, null, null, null);
    	return c;
    }
    
    
    
}
